﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Windows.Forms;

namespace Jabr.Logic.Roster
{
    [XmlRoot(ElementName = "abstract-acc")]
    public abstract class AbstractAccount : ICloneable, IComparable<AbstractAccount>
    {
        private DisplayableAccount m_disp;

        [XmlIgnore]
        public DisplayableAccount Display
        {
            get
            {
                return this.m_disp;
            }
            set
            {
                this.m_disp = value;
            }
        }

        protected bool m_new;
        protected string m_id;
        protected string m_user;
        protected string m_password;
        protected string m_host;
        protected int m_port;

        protected GenericPresence m_presence;
        protected string m_status;

        public AbstractAccount()
        {
            this.m_presence = GenericPresence.OFFLINE;
        }

        virtual public bool New
        {
            get
            {
                return this.m_new;
            }
            set
            {
                this.m_new = value;
            }
        }
        virtual public string UserID
        {
            get
            {
                return this.m_id;
            }
            set
            {
                this.m_id = value;
                if (this.OnUserIDChange != null)
                    this.OnUserIDChange();
            }
        }
        virtual public string Username
        {
            get
            {
                return this.m_user;
            }
            set
            {
                this.m_user = value;
                if (this.OnUsernameChange != null)
                    this.OnUsernameChange();
            }
        }
        virtual public string Password
        {
            get
            {
                return this.m_password;
            }
            set
            {
                this.m_password = value;
                if (this.OnPasswordChange != null)
                    this.OnPasswordChange();
            }
        }
        virtual public string Host
        {
            get
            {
                return this.m_host;
            }
            set
            {
                this.m_host = value;
            }
        }
        virtual public int Port
        {
            get
            {
                return this.m_port;
            }
            set
            {
                this.m_port = value;
            }
        }

        [XmlIgnore]
        virtual public GenericPresence Presence
        {
            get
            {
                return this.m_presence;
            }
            set
            {
                this.m_presence = value;
                if (this.OnPresence != null)
                    this.OnPresence(this.m_presence);
            }
        }
        virtual public string Status
        {
            get
            {
                return this.m_status;
            }
            set
            {
                this.m_status = value;
            }
        }
        abstract public string Type
        {
            get;
        }
        abstract public string Form
        {
            get;
        }

        public delegate void MessageDelegate(GenericMessage msg);
        public abstract event MessageDelegate OnMessageReceived;

        public delegate void PresenceDelegate(GenericPresence pre);
        public virtual event PresenceDelegate OnPresence;

        public delegate void RosterDelegate(IEnumerable<DisplayableItem> roster);
        public abstract event RosterDelegate OnRoster;

        public delegate void LoginDelegate(LoginResultType res, object e);
        public abstract event LoginDelegate OnLogin;
        public abstract event LoginDelegate OnRegister;

        public abstract void ContactRemove(string id);
        public abstract void ContactSubscribe(string id);
        public abstract void ContactUnsubscribe(string id);
        public abstract void ContactAdd(string id, string name, string group);
        public abstract void ContactUpdate(string id, string name, string group);

        public abstract void PresenceFrom(string id);
        public abstract void PresenceTo(string id, GenericPresence presence, string status);

        public abstract void ChatTo(string id, string chat);

        public delegate void PropertyDelegate();
        public virtual event PropertyDelegate OnUsernameChange;
        public virtual event PropertyDelegate OnUserIDChange;
        public virtual event PropertyDelegate OnPasswordChange;

        public abstract void Connect();
        public abstract void Disconnect();
        public abstract object Connection
        {
            get;
        }

        #region ICloneable Members

        abstract public object Clone();

        #endregion
        #region IComparable<AbstractAccount> Members

        abstract public int CompareTo(AbstractAccount other);

        #endregion
}
}
